<template>
  <el-dialog
    :title="id ? $t('common.modify') : $t('common.add')"
    :visible.sync="dialogVisible"
    width="500px"
    :show="show"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @open="open"
  >
    <el-form
      ref="form"
      :model="info"
      label-width="90px"
      :rules="rules"
      v-loading="infoLoading"
    >
      <div class="el-dialog-div form-scrollbar">
        <el-form-item
          :label="$t('common.preview')"
          v-if="info.color && info.name"
        >
          <el-tag
            :style="{
              'background-color': info.color,
              'border-color': info.color,
            }"
            effect="dark"
          >
            {{ info.name }}
          </el-tag>
        </el-form-item>
        <el-form-item :label="$t('common.name')" prop="name">
          <el-input
            type="text"
            v-model="info.name"
            :placeholder="$t('common.enter', [$t('customer.tagName')])"
            maxlength="255"
          />
        </el-form-item>

        <el-form-item :label="$t('common.color')" prop="color">
          <el-color-picker v-model="info.color"></el-color-picker>
        </el-form-item>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-row>
        <el-col :span="12" class="btnEdit">
          <el-button :disabled="infoLoading" type="primary" @click="subInfo">{{
            $t('common.save')
          }}</el-button>
        </el-col>
        <el-col :span="12" class="btnEdit">
          <el-button @click="handleClose">{{ $t('common.cancel') }}</el-button>
        </el-col>
      </el-row>
    </div>
  </el-dialog>
</template>

<script>
import {
  createLabel,
  getLabel,
  getList,
  updateLabel,
} from '@/api/customer/label'
import { createArticle } from '@/api/articles'

export default {
  name: 'edit',
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    id: '',
    closeAction: '',
  },
  data() {
    return {
      dialogVisible: this.show,
      isChange: false,
      initForm: false,
      // 页面元素
      info: {},
      tempData: {
        name: '',
        color: '',
      },
      infoLoading: true,
      rules: {
        //element form表单元素验证
        name: [
          {
            required: true,
            message: this.$t('common.enter', [this.$t('common.name')]),
            trigger: 'blur',
          },
        ],
        color: [
          {
            required: true,
            message: this.$t('common.select', [this.$t('common.color')]),
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    async open() {
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
      if (this.id != '') {
        const { data } = await getLabel(this.id)
        this.info = data
        console.log(data)
      } else {
        this.resetTempData()
        this.info = this.tempData
      }
      this.initForm = true
      setTimeout(() => {
        this.infoLoading = false
      }, 0.5 * 1000)
    },
    subInfo() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          this.infoLoading = true
          const data = this.info
          if (this.id != '') {
            await updateLabel(this.id, data)
              .then((res) => {
                this.$notify({
                  title: this.$t('common.success'),
                  message: this.$t('common.updateSuccessfully'),
                  type: 'success',
                  duration: 2000,
                })
                this.$emit('closeAction')
              })
              .catch(() => {
                setTimeout(() => {
                  this.infoLoading = false
                }, 0.5 * 1000)
              })
          } else {
            await createLabel(data)
              .then((res) => {
                this.$notify({
                  title: this.$t('common.success'),
                  message: this.$t('common.addSuccessfully'),
                  type: 'success',
                  duration: 2000,
                })
                this.$emit('closeAction')
              })
              .catch(() => {
                setTimeout(() => {
                  this.infoLoading = false
                }, 0.5 * 1000)
              })
          }
        }
      })
    },
    resetTempData() {
      this.tempData = {
        name: '',
        color: '',
      }
    },
    handleClose(done) {
      if (this.isChange) {
        this.$confirm(
          this.$t('common.unsavedChangesTip'),
          this.$t('common.tips'),
          {
            type: 'warning',
          }
        )
          .then((_) => {
            this.$emit('update:show', false)
          })
          .catch((_) => {})
      } else {
        this.$emit('update:show', false)
      }
    },
  },
  watch: {
    show() {
      this.dialogVisible = this.show
    },
    tempData: {
      handler: function () {
        if (this.initForm) {
          this.isChange = false
          this.initForm = false
        } else {
          this.isChange = true
        }
      },
      deep: true,
    },
  },
}
</script>

<style scoped>
.form-scrollbar {
  height: 200px !important;
}
</style>
